Skip to content

bgp: add local_as support to BGP_NEIGHBOR template#26556

Open
briantopping wants to merge 1 commit intosonic-net:masterfrom
briantopping:bgp-local-as-support
Open

bgp: add local_as support to BGP_NEIGHBOR template#26556
briantopping wants to merge 1 commit intosonic-net:masterfrom
briantopping:bgp-local-as-support

Conversation

@briantopping
Copy link
Copy Markdown

Summary

  • Add local_as attribute support to the general BGP neighbor instance template
  • When local_as is defined in BGP_NEIGHBOR CONFIG_DB table, renders neighbor <addr> local-as <asn> no-prepend replace-as in FRR config
  • Three-line template change, no Python code changes needed (bgpcfgd already passes all CONFIG_DB attributes to the template as bgp_session)

Motivation

Operators running iBGP internally with a private AS need to present a different (public) AS to upstream PE routers via local-as. This is a standard BGP deployment pattern for multi-site networks.

Dell Enterprise SONiC already supports the local_as attribute in BGP_NEIGHBOR and BGP_PEER_GROUP tables. This patch brings the same capability to community SONiC by adding the corresponding template logic.

Example CONFIG_DB usage

sonic-db-cli CONFIG_DB hset 'BGP_NEIGHBOR|default|10.0.0.1' local_as 64512

Produces in FRR:

neighbor 10.0.0.1 local-as 64512 no-prepend replace-as

Test plan

  • Verified on Quanta IX8-56X (BCM56870) with FRR 10.6-dev
  • PE BGP sessions establish correctly with local-as presenting AS 10595 while router runs AS 65001
  • Neighbors without local_as attribute are unaffected (conditional renders nothing)

🤖 Generated with Claude Code

Add support for the local_as attribute in the BGP_NEIGHBOR CONFIG_DB table.
When local_as is defined for a neighbor, the template renders:
  neighbor <addr> local-as <asn> no-prepend replace-as

This enables operators to present a different AS number to specific peers
while using a single internal AS for iBGP. Common use case: sites running
iBGP internally with a private AS while presenting a public AS to upstream
PE routers.

Matches the local_as attribute schema used in Dell Enterprise SONiC for
the BGP_NEIGHBOR and BGP_PEER_GROUP tables.
@briantopping briantopping requested a review from lguohan as a code owner April 4, 2026 03:22
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Apr 4, 2026

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: briantopping / name: Brian Topping (b13e249)

@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for configuring per-neighbor BGP local-AS in the FRR bgpd neighbor instance Jinja template.

Changes:

  • Conditionally renders a neighbor <addr> local-as <asn> ... line when a local-AS attribute is present.
  • Leaves existing neighbor rendering (remote-as, description, timers, etc.) unchanged.

!
neighbor {{ neighbor_addr }} remote-as {{ bgp_session['asn'] }}
neighbor {{ neighbor_addr }} description {{ bgp_session['name'] }}
{% if bgp_session['local_as'] is defined %}
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template checks/uses bgp_session['local_as'], but the upstream CONFIG_DB/YANG schema for BGP neighbors uses local_asn (see src/sonic-yang-models/yang-models/sonic-bgp-common.yang and src/sonic-yang-models/tests/files/sample_config_db.json). As written, setting local_asn on BGP_NEIGHBOR will not render any neighbor ... local-as ... line. Consider switching to local_asn (and optionally supporting both keys for backward compatibility).

Suggested change
{% if bgp_session['local_as'] is defined %}
{% if bgp_session['local_asn'] is defined %}
neighbor {{ neighbor_addr }} local-as {{ bgp_session['local_asn'] }} no-prepend replace-as
{% elif bgp_session['local_as'] is defined %}

Copilot uses AI. Check for mistakes.
neighbor {{ neighbor_addr }} remote-as {{ bgp_session['asn'] }}
neighbor {{ neighbor_addr }} description {{ bgp_session['name'] }}
{% if bgp_session['local_as'] is defined %}
neighbor {{ neighbor_addr }} local-as {{ bgp_session['local_as'] }} no-prepend replace-as
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neighbor ... local-as ... no-prepend replace-as is rendered unconditionally whenever local_as is set. CONFIG_DB/YANG also defines local_as_no_prepend and local_as_replace_as flags, and hard-coding both options changes semantics vs plain local-as for users who only want one (or neither). Consider rendering no-prepend/replace-as conditionally based on those attributes (defaulting to FRR defaults when unspecified).

Suggested change
neighbor {{ neighbor_addr }} local-as {{ bgp_session['local_as'] }} no-prepend replace-as
neighbor {{ neighbor_addr }} local-as {{ bgp_session['local_as'] }}{% if bgp_session['local_as_no_prepend'] is defined and bgp_session['local_as_no_prepend'] | int != 0 %} no-prepend{% endif %}{% if bgp_session['local_as_replace_as'] is defined and bgp_session['local_as_replace_as'] | int != 0 %} replace-as{% endif %}

Copilot uses AI. Check for mistakes.
Comment on lines +6 to +7
{% if bgp_session['local_as'] is defined %}
neighbor {{ neighbor_addr }} local-as {{ bgp_session['local_as'] }} no-prepend replace-as
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new template behavior isn’t covered by existing bgpcfgd unit tests (e.g., src/sonic-bgpcfgd/tests/test_bgp.py adds peers but doesn’t assert the rendered config for new attributes). Adding a test that sets local_asn (and the optional local_as_no_prepend / local_as_replace_as flags if supported) and asserts the generated neighbor ... local-as ... line would prevent regressions.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants